package com.lc.pianostudent.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.bean.PianoRecordBean;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class HomeNewsAdapter extends RecyclerView.Adapter<HomeNewsAdapter.ViewHolder>{


    private Context context;

    private List<HomeNewsBean> list = new ArrayList<>();

    public HomeNewsAdapter(Context context, List<HomeNewsBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private HomeNewsAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(HomeNewsAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_news, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        HomeNewsAdapter.ViewHolder viewHolder = new HomeNewsAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(list.get(position).getClassid().equals("1")){
            holder.type.setText("新闻");
            holder.type.setTextColor(Color.parseColor("#00adff"));
            holder.type.setBackgroundResource(R.drawable.shape_blue_storke);
        }else {
            holder.type.setText("视频");
            holder.type.setTextColor(Color.parseColor("#ff0000"));
            holder.type.setBackgroundResource(R.drawable.shape_red_storke);
        }


        holder.time.setText(list.get(position).getCreate_time());

        holder.title.setText(list.get(position).getTitle());
        if(list.get(position).getTop().equals("1") || list.get(position).getCrude().equals("1")){
            holder.title.setTextColor(Color.parseColor(list.get(position).getColor()));
            TextPaint tp =  holder.title.getPaint();
            tp.setFakeBoldText(true);
        }else {
            holder.title.setTextColor(Color.parseColor("#191919"));
            TextPaint tp =  holder.title.getPaint();
            tp.setFakeBoldText(false);
        }

//        Glide.with(context).load(list.get(position).getPicurl()).into(holder.img);
        GlideLoader.getInstance().get(list.get(position).getPicurl(),holder.img,R.mipmap.shouye);
        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final HomeNewsAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title,time,type;
        public ImageView img;

        public ViewHolder(View itemView) {
            super(itemView);

            type = (TextView) itemView.findViewById(R.id.type);
            time = (TextView) itemView.findViewById(R.id.time);
            title = (TextView) itemView.findViewById(R.id.title);
            img = itemView.findViewById(R.id.news_img);

        }
    }
}
